import axios from 'axios';

export const fetchList=(data)=>{
    return {
        type:"GETLISTS",
        data
    }
}

export function getListData(params={page:1,limit:8}){
    return dispatch=>{
        var start =20*(params.page-1);
        var url=`http://localhost:3000/product/list`;
        return fetch(url, {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
          })
        .then(res=>{
            return res.json()
        })
        .then((data)=>{
            dispatch(fetchList(data));
        })
    }
}


export const toGetCarts=(data)=>{
    return {
        type:"GETCARTS",
        data
    }
}

export function getCarts(userId){
    return dispatch=>{
		return axios({
            url:'http://localhost:3000/carts/list',
            method: 'POST',
            data: {
                userId:userId
            }
        })
        .then((res)=>{
            dispatch(toGetCarts(res.data.rows));
        })
    }
}



export function addToCart(params){
	return dispatch=>{
		dispatch({
            type:"ADDTOCART",
            data:params
        })
	}
}

export function reduceNum(params){
	return dispatch=>{
		dispatch({
            type:"REDUCENUM",
            data:params
        })
	}
}

export function delPdt(params){
	return dispatch=>{
		dispatch({
            type:"DELETE",
            data:params
        })
	}
}


export function saveUserId(data){
    return {
        type:'SAVEUSERID',
        data
    }
}
